package com.prj.ufdm.demo.schedule;

import java.util.Date;

import org.springframework.context.annotation.PropertySource;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;

import com.prj.ufdm.demo.constant.DemoConstants;

@PropertySource("classpath:application.properties")
//@Component
@Order(99)
public class ScheduleTask implements SchedulingConfigurer{

	/**
	  * 通过注解实现
	 * @throws Exception
	 */
//	@Scheduled(cron="${job.schedule}") 
    public void testTaskExcute() throws Exception {
		System.out.println("\n 开始启动执行");
    }

	/**
	 * 通过接口实现
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addTriggerTask(new Runnable() {
			@Override
			public void run() {
				System.out.println("\n"+Thread.currentThread().getName() + " 开始执行" + new Date());
			}
		},new Trigger() {
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				// 通过 fixedRate 执行
				PeriodicTrigger trigger = new PeriodicTrigger(DemoConstants.JOB_PERIOD);
				// 通过  cron 表达式执行
//				CronTrigger trigger = new CronTrigger(DemoConstants.JOB_SCHEDULE);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                return nextExec;
			}
		});
	}

}
